package utils.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;
import com.lanou.horo.myapplication.dbdao.CollectionDBEntity;

import java.util.List;

import volley.VolleySingleton;

/**
 * Created by dllo on 15/12/1.
 */
public class MyColltionAdpter extends BaseAdapter {
    private List<CollectionDBEntity> entities;
    LayoutInflater inflater;

    public void addData( LayoutInflater inflater,List<CollectionDBEntity> entities){
        this.inflater = inflater;
        this.entities = entities;
        notifyDataSetChanged();
    }
    public boolean isCount(){
        return  entities!= null && entities.size() > 0 ;
    }

    @Override
    public int getCount() {
        return entities!=null && entities.size()>0 ? entities.size() :0;
    }

    @Override
    public Object getItem(int position) {
        return entities.size();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isCount()){
            ViewHolder holder = null;
            if (convertView == null){
                holder = new ViewHolder();
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                convertView  =inflater.inflate(R.layout.collection_item,null);
                //初始化控件
                holder.collectionContent= (TextView) convertView .
                        findViewById(R.id.collectionItemImgItemTopContent);
                holder.collectionItemImgItemTopTitle = (TextView) convertView.
                        findViewById(R.id.collectionItemImgItemTopTitle);
                holder.collectionItemImg = (NetworkImageView) convertView.
                        findViewById(R.id.collectionItemImg);

                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.collectionContent.setText(entities.get(position).getNickname());
            VolleySingleton.getInstance().getBitmap(entities.get(position).getSmallLogo(),
                    holder.collectionItemImg);
            holder.collectionItemImgItemTopTitle.setText(entities.get(position).getTitle());
        }
        return convertView;
        }


    //匿名内部类
    class ViewHolder {
        private TextView collectionContent,collectionItemImgItemTopTitle;
        private NetworkImageView collectionItemImg;
    }
}
